var app = getApp(), api = require("../../../../api.js");
Page({
  data: {
    panel: 'panel1',
    num_day:'',
    num_wee:'' ,
    num_month:'' ,
    num_total: '', 
    price_day:'',
    price_week:'',
    price_month:'',
    price_total:'',
    arrweek:null,
    arrmonth:null,
    incomeweek: null,
    incomemonth: null,
    // arr7: [{}]
  },
  onLoad: function (options) {
    var that = this;
    var uid=wx.getStorageSync('uid');
    app.request({
      url: api.default.statistics,
      data: {
        uid: uid
      },
      method: "post",
      success: function (res) {
        let orderArr7 = [];
        for (let i = 0; i <= res.arr7.length-1; i++) {
          let obj7 = {};
          obj7.time = res.arr7[i];
          obj7.size = res.num7[i];
          orderArr7.push(obj7);
        }
        let orderArr30 = [];
        for (let j = 0; j <= res.arr30.length-1; j++) {
          let obj30 = {};
          obj30.time = res.arr30[j];
          obj30.size = res.num30[j];
          orderArr30.push(obj30);
        }
        let incomeArr7 = [];
        for (let k = 0; k <= res.prcie7.length - 1; k++) {
          let obja7 = {};
          obja7.time = res.arr7[k];
          obja7.size = res.prcie7[k];
          incomeArr7.push(obja7);
        }
        let incomeArr30 = [];
        for (let v = 0; v <= res.prcie30.length - 1; v++) {
          let obja30 = {};
          obja30.time = res.arr30[v];
          obja30.size = res.prcie30[v];
          incomeArr30.push(obja30);
        }
        that.setData({
          arrweek: orderArr7,
          arrmonth: orderArr30,
          incomeweek: incomeArr7,
          incomemonth: incomeArr30,
          num_day: res.num.num_day,
          num_week: res.num.num_week,
          num_month: res.num.num_month,
          num_total: res.num.num_total,
          price_day: res.num.price_day,
          price_week: res.num.price_week,
          price_month: res.num.price_month,
          price_total: res.num.price_total
        })
        that.drawCanvas({ canvasId: 'order-map1', data: that.data.arrweek, power: 100, showText: true });
        that.drawCanvas({ canvasId: 'order-map2', data: that.data.arrmonth, power: 100, showText: false });
        that.drawCanvas({ canvasId: 'income-map1', data: that.data.incomeweek, power: 150, showText: true });
        that.drawCanvas({ canvasId: 'income-map2', data: that.data.incomemonth, power: 150, showText: false });
      }
    })
  },
  switchPanel: function(e){
    var that = this;
    var panelVal = e.currentTarget.dataset.panel;
    that.setData({
      panel: panelVal
    });
  },
  /**
   * config{ 
   *  canvasId: canvasId
   *  dataArr：数据数组
   *  power: 数据最大值
   *  showText： 是否需要显示文字
   * }
   */
  drawCanvas: function(config){
    // 参数
    var id = config.canvasId;
    var arr = config.data;
    var power = config.power;
    var showText = config.showText;
    var canvasW = (wx.getSystemInfoSync().windowWidth / 750) * 700;
    var canvasH = (wx.getSystemInfoSync().windowWidth / 750) * 250;
    var lineX = 5;
    var lineY = arr.length;

    var orderMap1 = wx.createCanvasContext(id);

    orderMap1.setStrokeStyle('#ccc');
    orderMap1.setFillStyle('#14B5FA');
    orderMap1.setFontSize(8);

    for (var i = lineX; i > 0; i--) {
      orderMap1.beginPath();
      orderMap1.lineTo(0, canvasH / lineX * i)
      orderMap1.lineTo(canvasW, canvasH / lineX * i);
      orderMap1.stroke();

      orderMap1.fillText(10 - 2 * i, 0, canvasH / lineX * i)
    }


    for (var i = lineY; i >= 0; i--) {
      orderMap1.beginPath();
      orderMap1.lineTo(canvasW / lineY * i, 0)
      orderMap1.lineTo(canvasW / lineY * i, canvasH);
      orderMap1.stroke();
      if(showText){
        if (i < arr.length) {
          orderMap1.fillText(arr[i].time, canvasW / lineY * i + (canvasW / lineY / 3), canvasH - 5);
        }
      }
    }

    // 绘数据
    orderMap1.setStrokeStyle('#f90');
    orderMap1.beginPath();
    for (var i = 0; i < lineY; i++) {
      orderMap1.lineTo(canvasW / lineY * (i + 1), canvasH - canvasH / power * arr[i].size);
    }
    orderMap1.stroke();

    orderMap1.draw();
    orderMap1.save();
  }

})